﻿using System.Text;
using CustomerContactSystem.Core.Infrastructure.Domain;

namespace CustomerContactSystem.Core.Model {
    public class Customer : Entity {
        public virtual string FirstName { get; set; }
        public virtual string SecondName { get; set; }
        public virtual string Address1 { get; set; }
        public virtual string Address2 { get; set; }
        public virtual string Address3 { get; set; }
        public virtual string Address4 { get; set; }
        public virtual string Address5 { get; set; }
        public virtual string Email { get; set; }
        public virtual string Phone { get; set; }
        public virtual string Fax { get; set; }
        public virtual bool CanBeContactedForFeedback { get; set; }
        public virtual bool Flagged { get; set; }
        public virtual string FullName {
            get { return FirstName + " " + SecondName; }
        }
        public virtual string AddressLine {
            get {
                var sb = new StringBuilder();
                sb.Append(Address1);
                if (!string.IsNullOrEmpty(Address2)) {
                    sb.Append(", " + Address2);
                }
                if (!string.IsNullOrEmpty(Address3)) {
                    sb.Append(", " + Address3);
                }
                if (!string.IsNullOrEmpty(Address4)) {
                    sb.Append(", " + Address4);
                }
                if (!string.IsNullOrEmpty(Address5)) {
                    sb.Append(", " + Address5);
                }
                return sb.ToString();
            }
        }

        protected override void Validate() {
            if (string.IsNullOrEmpty(FirstName)) {
                AddBrokenRule(new BusinessRule("FirstName", "First name is required!"));
            }

            if (string.IsNullOrEmpty(SecondName)) {
                AddBrokenRule(new BusinessRule("SecondName", "Second name is required!"));
            }

            if (string.IsNullOrEmpty(Address1)) {
                AddBrokenRule(new BusinessRule("Address1", "Addres 1 is required!"));
            }
        }
    }
}